#include "qabstractgraphics.h"

#include "../../libs/shared/property/qabstractproperty.h"
#include "../../libs/shared/qpropertyfactory.h"
#include "../../libs/shared/qcommonstruct.h"
#include "../../libs/shared/qhostfactory.h"

QAbstractGraphicsWidget::QAbstractGraphicsWidget(QWidget *parent):
    QWidget(parent)
{
}

void QAbstractGraphicsWidget::setPen(tagPenInfo pen)
{
    if(pen.m_width<=0)
    {
        return;
    }
    QPen p=m_pen;
    p.setStyle(pen.m_pen_style);
    p.setWidth(pen.m_width);
    p.setCapStyle(pen.m_pen_cap_style);
    p.setJoinStyle(pen.m_pen_join_style);
    p.setBrush(QBrush(pen.m_color));

    if(m_pen!=p)
    {
        m_pen_info=pen;
        m_pen=p;
        update();
    }
}

tagPenInfo QAbstractGraphicsWidget::pen()
{
    return m_pen_info;
}

void QAbstractGraphicsWidget::setAntialiasing(bool antialiasing)
{
    if(m_antialiasing!=antialiasing)
    {
        m_antialiasing=antialiasing;
        update();
    }
}

bool QAbstractGraphicsWidget::antialiasing()
{
    return m_antialiasing;
}

QAbstractGraphicsHost::QAbstractGraphicsHost(QAbstractHost *parent):
    QWidgetHost(parent)
{
    setProperty("need_frame",true);
    setProperty("accept_drop",false);

    QStringList list=property("function_list").toStringList();
    list<<"set_focus()"<<"kill_focus()"<<"setEnabled(bool)"<<"enabled()"<<"setToolTip(QString)"
       <<"toolTip()"<<"setCursor(int)"<<"cursor()";
    setProperty("function_list",list);
}


void QAbstractGraphicsHost::init_property()
{
    QWidgetHost::init_property();

    remove_property("cursor");
    remove_property("enabled");
    remove_property("focusPolicy");
    remove_property("background");
    remove_property("border");
    remove_property("toolTip");
    remove_property("mouseMove");
    remove_property("mousePress");
    remove_property("mouseRelease");
    remove_property("doubleClick");

    QAbstractProperty* pro;

    pro=get_property("font");
    if(pro!=NULL)
    {
        pro->set_attribute(ATTR_VISIBLE,false);
    }

    pro=QPropertyFactory::create_property("Pen");
    if(pro!=NULL)
    {
        pro->set_property("name","pen");
        pro->set_attribute("show_name",tr("Pen"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("pen",pro->get_value());
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Bool");
    if(pro!=NULL)
    {
        pro->set_property("name","antialiasing");
        pro->set_attribute("show_name",tr("Antialiasing"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("antialiasing",false);
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }
}

void QAbstractGraphicsHost::setPenWidth(int penWidth)
{
    tagPenInfo info=get_property_value("pen").value<tagPenInfo>();
    info.m_width=penWidth;
    set_property_value("pen",QVariant::fromValue<tagPenInfo>(info));
}

int QAbstractGraphicsHost::penWidth()
{
    tagPenInfo info=get_property_value("pen").value<tagPenInfo>();
    return info.m_width;
}

void QAbstractGraphicsHost::setPenStyle(int penStyle)
{
    tagPenInfo info=get_property_value("pen").value<tagPenInfo>();
    info.m_pen_style=(Qt::PenStyle)penStyle;
    set_property_value("pen",QVariant::fromValue<tagPenInfo>(info));
}

int QAbstractGraphicsHost::penStyle()
{
    tagPenInfo info=get_property_value("pen").value<tagPenInfo>();
    return info.m_pen_style;
}

void QAbstractGraphicsHost::setCapStyle(int capStyle)
{
    tagPenInfo info=get_property_value("pen").value<tagPenInfo>();
    info.m_pen_cap_style=(Qt::PenCapStyle)capStyle;
    set_property_value("pen",QVariant::fromValue<tagPenInfo>(info));
}

int QAbstractGraphicsHost::capStyle()
{
    tagPenInfo info=get_property_value("pen").value<tagPenInfo>();
    return info.m_pen_cap_style;
}

void QAbstractGraphicsHost::setJoinStyle(int joinStyle)
{
    tagPenInfo info=get_property_value("pen").value<tagPenInfo>();
    info.m_pen_join_style=(Qt::PenJoinStyle)joinStyle;
    set_property_value("pen",QVariant::fromValue<tagPenInfo>(info));
}

int QAbstractGraphicsHost::joinStyle()
{
    tagPenInfo info=get_property_value("pen").value<tagPenInfo>();
    return info.m_pen_join_style;
}

void QAbstractGraphicsHost::setAntialiasing(bool antialiasing)
{
    set_property_value("antialiasing",antialiasing);
}

bool QAbstractGraphicsHost::antialiasing()
{
    return get_property_value("antialiasing").toBool();
}
